<?php
// 后台用户管理
namespace NewAdmin\Controller;

use Think\Controller;

class StoreController extends CommonController {
	//店铺管理
	public function store_list(){
		$storeInfo = D('store');
		$title = $_GET['title'];
		
		if($title === iconv('utf-8', "utf-8", iconv("utf-8", 'utf-8', $title))){
			$title=$title;
		}else{
			$title= iconv("gbk","utf-8",$title);
		}
		if($title){
			$where['title'] = array('like',"%$title%");
		}
		if($_GET['status']!=null){
			$where['status']=$_GET['status'];
		}
		if($_GET['recommend']){
			$where['recommend']=$_GET['recommend'];
		}
		if($_GET['addtime']&&$_GET['addtime1']){
			$addtime=strtotime($_GET['addtime']);
  			$addtime1=strtotime($_GET['addtime1']);
			$where['addtime']=array('between',array($addtime,$addtime1));
		}
		if($_GET['addtime']==null&&$_GET['addtime1']){
			$addtime=strtotime($_GET['addtime']);
			$where['addtime']=array('ELT',$addtime);
		}
		if($_GET['addtime']&&$_GET['addtime1']==null){
			$addtime1=strtotime($_GET['addtime1']);
			$where['addtime']=array('EGT',$addtime1);
		}
		$userArray = $this->userArray;
		$count =$storeInfo->where($where)->count();
		$Page = new \Think\Page ($count, 15 ); // 实例化分页类 传入总记录数
		$show = $Page->show1(); // 分页显示输出
		$storeArray = $storeInfo->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('page', $show ); // 赋值分页输出
		$this->assign('count',$count);
		$this->assign('storeArray', $storeArray );
		$this->assign('title', '店铺列表' );
		$this->display();
	}
	//修改状态
	public function storeSave(){
		$data=I('get.');
		$type=$data['type'];
		$storeInfo = D('store');
		$store_info=$storeInfo->find($data['id']);
		if($store_info[$type]==0){
			$info[$type]=1;
			$info['id']=$data['id'];
		}else{
			$info[$type]=0;
			$info['id']=$data['id'];			
		}
		$rs=$storeInfo->save($info);
		if($rs){
			die('0|||操作成功|||/Store/store_list');
		}
	}
	public function storeAdd(){
		$storeInfo	= D('store');
		$id = I('get.id',0);
		$store_classification=M('store_classification');
    	$store_classifications=$store_classification->select();
    	$this->assign('store_classifications',$store_classifications);
		if($id){
			$current = $storeInfo->where('id='.$id)->find();
			$current['newpassword']=passport_decrypt($current['password'],'wnsc');
			$user=M('users')->find($current['uid']);

            $options   =   D('options');
            $option_name='wx_store_'.$id;
            //获取小程序配置
            $w_data['option_name']=$option_name;
            $new_w_data=$options->where($w_data)->find();
            $w_config=json_decode($new_w_data['option_value']);
            $smallRoutineInfo=get_object_vars($w_config);
			$this->assign ( 'title', '编辑品牌店' );
			$this->assign ( 'smallRoutineInfo',$smallRoutineInfo );
			$this->assign ( 'current',$current );
			$this->assign ( 'user',$user );
		}else{
			$this->assign ( 'title', '添加品牌店' );
		}
		$this->display();
	}
	public function store_Save(){
		$data=I('post.');

		$data['store_classification_type']=implode(',',$data['store_classification_type']);
		if($data['extraction']){
			$data['extraction']=$data['extraction']/100;
		}
		if($data['spread']=='on'){
			$data['spread']=1;
		}else{
			$data['spread']=0;
		}
		if($data['recommend']=='on'){
			$data['recommend']=1;
		}else{
			$data['recommend']=0;
		}
		if($data['status']=='on'){
			$data['status']=1;
		}else{
			$data['status']=0;
		}
        if($data['quanwang']=='on'){
            $data['quanwang']=1;
        }else{
            $data['quanwang']=0;
        }
		$storeInfo	= D('store');
		if($data['id']){
			$storeInfo->save($data);
            $options   =   D('options');
            $option_name='wx_store_'.$data['id'];
            $map['option_name']='wx_store_'.$data['id'];
            $wx_store_data['option_name']='wx_store_'.$data['id'];
            $wx_store_data['APP_KEY']=$data['APP_KEY'];
            $wx_store_data['APP_SECRET']=$data['APP_SECRET'];
            $wx_store_data['MCHID']=$data['MCHID'];
            $wx_store_data['MCHID_KEY']=$data['MCHID_KEY'];
            $data_json=json_encode($wx_store_data);
            $new_data['option_name']='wx_store_'.$data['id'];
            $new_data['option_value']=$data_json;
            $new_data['autoload']=1;

            if($options->where($map)->find()){
                $r=$options->where($map)->save($new_data);
            }else{
                $r=$options->add($new_data);
            }
            /*if($r){
                $login[$option_name]=$wx_store_data;
                file_put_contents('data/conf/wechatApi.php', "<?php\treturn " . var_export($login, true) . ";");
            }*/
			echo "0|||更新成功|||reload";
		}else{
			$data['addtime']=time();
			$data['password']=passport_encrypt($data['password'],'wnsc');
			$store_id=$storeInfo->add($data);
			if($store_id){
                $options   =   D('options');
                $option_name='wx_store_'.$store_id;
                $map['option_name']=$option_name;
                $wx_store_data['option_name']=$option_name;
                $wx_store_data['APP_KEY']=$data['APP_KEY'];
                $wx_store_data['APP_SECRET']=$data['APP_SECRET'];
                $wx_store_data['MCHID']=$data['MCHID'];
                $wx_store_data['MCHID_KEY']=$data['MCHID_KEY'];
                $data_json=json_encode($wx_store_data);
                $new_data['option_name']=$option_name;
                $new_data['option_value']=$data_json;
                $new_data['autoload']=1;
                if($options->where($map)->find()){
                    $r=$options->where($map)->save($new_data);
                }else{
                    $r=$options->add($new_data);
                }
                /*if($r){
                    $login[$option_name]=$wx_store_data;
                    file_put_contents('data/conf/wechatApi.php', "<?php\treturn " . var_export($login, true) . ";");
                }*/
            }
			echo "0|||添加成功|||reload";
		}
	}
	public function search_phone(){
		$phone=I('get.phone');
		$users=M('users');
		$rs=$users->where('phone='.$phone)->find();
		if($rs){
			$store=M('store');
			$c_rs=$store->where('uid='.$rs['id'])->select();
			if($c_rs){
				$this->ajaxReturn(1);
			}else{
				$this->ajaxReturn($rs);
			}
		}else{
			$this->ajaxReturn(0);
		}
		
	}

    public function search_phone2(){
        $phone=I('get.phone');
        $users=M('users');
        $rs=$users->where('phone='.$phone)->find();
        if($rs){
            $partner=M('partner');
            $c_rs=$partner->where('u_id='.$rs['id'])->select();
            if($c_rs){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn($rs);
            }
        }else{
            $this->ajaxReturn(0);
        }

    }


	public function store_classification()
    {
    	$store_classification=M('store_classification');
    	$store_classifications=$store_classification->select();
    	$this->assign('store_classifications',$store_classifications);
    	$this->assign('title','店铺分类');
    	$this->display();
    }
    
    public function store_classificationAdd()
    {
    	$store_classification=M('store_classification');
    	if($_POST){
    		$data=I('post.');
    		if($data['id']){
    			$rs=$store_classification->save($data);
    			echo "0|||保存成功|||reload";die;
    		}else{
    			
    			$rs=$store_classification->add($data);
    			echo "0|||添加成功|||reload";die;
    		}
    		
    	}else{
    		if($_GET['id']){
    			$current=$store_classification->find($_GET['id']);
    			$this->assign('current',$current);
    		}
	    	$this->assign('title','添加店铺分类');
	    	$this->display();
    	}
    	
    }
  
   public function store_classificationdel(){
   		if($_GET['id']){
   			$store_classification=M('store_classification');
    		$current=$store_classification->delete($_GET['id']);
    		$this->redirect('Store/store_classification');
    	}
   }
	 /**
     * 获取省列表
     */
    public function getProvince()
    {
    	$province_list = getProvinceList();
    	$this->ajaxReturn($province_list);
    }

    /**
     * 获取城市列表
     *
     * @return Ambigous <multitype:\think\static , \think\false, \think\Collection, \think\db\false, PDOStatement, string, \PDOStatement, \think\db\mixed, boolean, unknown, \think\mixed, multitype:, array>
     */
    public function getCity()
    {
        
        $province_id = isset($_POST['province_id']) ? $_POST['province_id'] : 0;
        $city_list = getCityList($province_id);
        $this->ajaxReturn($city_list);
       
    }

    /**
     * 获取区域地址
     */
    public function getDistrict()
    {
       
        $city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;
        $district_list = getDistrictList($city_id);
        $this->ajaxReturn($district_list);
       
    }

    /**
     * 获取选择地址
     *
     * @return unknown
     */
    public function getSelectAddress()
    {
        $address = new Address();
        $province_list = getProvinceList();
        $province_id = isset($_POST['province_id']) ? $_POST['province_id'] : 0;
        $city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;
        $city_list = getCityList($province_id);
        $district_list =getDistrictList($city_id);
        $data["province_list"] = $province_list;
        $data["city_list"] = $city_list;
        $data["district_list"] = $district_list;
         $this->ajaxReturn($data);
    }

    public function  join(){

        $count =M('store_join')->count();
        $Page = new \Think\Page ($count, 13 ); // 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show = $Page->show(); // 分页显示输出
        $rs=M('store_join')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page', $show ); // 赋值分页输出
        $this->assign('count',$count);
        $this->assign('rs', $rs );
        $this->assign('title', '厂商入驻' );
        $this->display();
    }

    public  function  edit_join(){
        $id=I('get.id');
        $data['id']=$id;
        $data['status']=1;
        $rs=M('store_join')->save($data);
        $this->redirect('store/join');
    }

	
}